package com.vooda.business_admin.config;


import com.vooda.business_admin.entity.SysUser;
import com.vooda.business_admin.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * author: Charlee,
 * date: 2017/3/24,
 * version: v1.0.0
 */
@Component
public class CustomerAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private CdnProperties cdnProperties;

    public CustomerAuthenticationSuccessHandler(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        SysUser byAccount = sysUserService.findByUserName(authentication.getName());
        HttpSession session = request.getSession();
        session.setAttribute("sysUser", byAccount);
        session.setAttribute("currentUserId", byAccount.getId());
        session.setAttribute("cdn", cdnProperties.getBaseUrl());
        response.sendRedirect("management");
    }
}
